package com.liuyucheng.concurrent.callable;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class TestCallable implements Callable<String> {

    private static ExecutorService executor = Executors.newFixedThreadPool(10);

    @Override
    public String call() throws Exception {
        for (int i = 0; i < 10; i++) {
            System.out.println("i = " + i);
            Thread.sleep(500);
        }
        return "执行完成";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //异步执行方法
//        CompletableFuture.runAsync(()->{
//            for (int i = 0; i < 10; i++) {
//                System.out.println("i = " + i);
//                try {
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        },executor);



        //方法执行完成后的感知
//        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
//            for (int i = 0; i < 10; i++) {
//                System.out.println("i = " + i);
//                try {
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//            return "执行完成";
//        }, executor)
//                //当完成执行后，可以设置一个回调方法
//                .whenComplete((result,exception)->{
//                    //这里虽然能收到异常信息，但是与exceptionally不同，这里不能修改返回值
//                    log.info("异步任务成功完成了,结果：{}，异常：{}",result,exception);
//                })
//                //出现异常时的回调
//                .exceptionally(throwable -> {
//                    return "默认的返回值";
//                });



        //使用handle对异常进行处理
//        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
//                    for (int i = 0; i < 10; i++) {
//                        System.out.println("i = " + i);
//                        "".charAt(3);
//                        try {
//                            Thread.sleep(500);
//                        } catch (InterruptedException e) {
//                            throw new RuntimeException(e);
//                        }
//                    }
//                    return "执行完成";
//                }, executor)
//                //
//                .handle((result,throwable)->{
//                    if(result!=null){
//                        return "方法成功执行完成";
//                    }
//                    if(throwable!=null){
//                        return "方法执行失败,"+ throwable;
//                    }
//                    return "默认的返回值";
//                });


        //线程串行化,thenRunAsync,不能获取到上一步的执行结果
//        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
//            for (int i = 0; i < 10; i++) {
//                System.out.println("i = " + i);
//                try {
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//            return "执行完成";
//        }, executor).thenRunAsync(() -> {
//            System.out.println("任务2启动并完成了");
//        });



        //thenAcceptAsync可以获取到上一个任务的执行结果
//        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
//            for (int i = 0; i < 10; i++) {
//                System.out.println("i = " + i);
//                try {
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//            return "执行完成";
//        }, executor).thenAcceptAsync((result) -> {
//            System.out.println("result = " + result);
//            System.out.println("任务2启动并完成了");
//        });



        //thenAcceptAsync可以获取到上一个任务的执行结果
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            for (int i = 0; i < 10; i++) {
//                System.out.println("i = " + i);
//                try {
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//            return "执行完成";
//        }, executor).thenApplyAsync((result) -> {
//            System.out.println("result = " + result);
//            System.out.println("任务2启动并完成了");
//            return "任务2执行完成";
//        });


//        CompletableFuture<String> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务1执行完成");
//            return "执行1返回值";
//        }, executor);
//        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务2执行完成");
//            return "执行2返回值";
//        }, executor);
////        future01.runAfterBothAsync(future02,()->{
////            System.out.println("任务3开始,此时任务1和2都已经结束");
////        },executor);
////        future01.thenAcceptBothAsync(future02,(f1,f2)->{
////            System.out.println("任务3开始,此时任务1和2都已经结束,之前的结果:"+f1+","+f2);
////        },executor);
//        CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
//            System.out.println("任务3开始,此时任务1和2都已经结束,之前的结果:" + f1 + "," + f2);
//            return "执行3返回值";
//        }, executor);
//        System.out.println("future = " + future.get());





        //anyof同理
        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "xxx.jpg";
        }, executor);
        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性信息");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "黑色+256G";
        }, executor);
        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的介绍");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "华为";
        }, executor).whenComplete((result, ex) -> {
            log.info("result:{}",result);
            log.info("ex:",ex);
        });
        CompletableFuture<Void> completableFuture = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
        completableFuture.get();
        System.out.println("所有任务结束");


//        System.out.println("completableFuture.get() = " + future.get());
    }

//    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        Callable<String> callable = new TestCallable();
//        FutureTask<String> futureTask = new FutureTask<>(callable);
//        new Thread(futureTask).start();
//
//        //futuretask可以拿到线程的运行结果，而继承thread和实现runnable接口不行
//        String result = futureTask.get();
//        System.out.println("result = " + result);
//    }
}
